
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{color}
\usepackage[ascii]{inputenc}



\makeatletter
\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
    \let\PY@ul=\relax \let\PY@tc=\relax%
    \let\PY@bc=\relax \let\PY@ff=\relax}
\def\PY@tok#1{\csname PY@tok@#1\endcsname}
\def\PY@toks#1+{\ifx\relax#1\empty\else%
    \PY@tok{#1}\expandafter\PY@toks\fi}
\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
    \PY@it{\PY@bf{\PY@ff{#1}}}}}}}
\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}

\def\PY@tok@gu{\let\PY@bf=\textbf}
\def\PY@tok@gs{\let\PY@bf=\textbf}
\def\PY@tok@cm{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@gp{\let\PY@bf=\textbf}
\def\PY@tok@ge{\let\PY@it=\textit}
\def\PY@tok@cs{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@cp{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@gh{\let\PY@bf=\textbf}
\def\PY@tok@s2{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@s1{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@nc{\def\PY@tc##1{\textcolor[rgb]{0.17,0.57,0.69}{##1}}}
\def\PY@tok@si{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@sh{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@sc{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@c1{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@kc{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@c{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@sx{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@err{\def\PY@bc##1{\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{##1}}}
\def\PY@tok@kd{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@ss{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@sr{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@k{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@kn{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@kr{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@s{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@kp{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@kt{\def\PY@tc##1{\textcolor[rgb]{0.17,0.57,0.69}{##1}}}
\def\PY@tok@ow{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@sb{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@se{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}
\def\PY@tok@sd{\def\PY@tc##1{\textcolor[rgb]{0.64,0.08,0.08}{##1}}}

\def\PYZbs{\char`\\}
\def\PYZus{\char`\_}
\def\PYZob{\char`\{}
\def\PYZcb{\char`\}}
\def\PYZca{\char`\^}
\def\PYZsh{\char`\#}
\def\PYZpc{\char`\%}
\def\PYZdl{\char`\$}
\def\PYZti{\char`\~}
% for compatibility with earlier versions
\def\PYZat{@}
\def\PYZlb{[}
\def\PYZrb{]}
\makeatother


\begin{document}

\section*{}

\begin{Verbatim}[commandchars=\\\{\}]
\PY{k}{public} \PY{k}{class} \PY{n+nc}{WidgetComposition} \PY{p}{:} \PY{n}{IWidget} \PY{p}{\PYZob{}}
  \PY{k}{private} \PY{n}{IList}\PY{p}{<}\PY{n}{IWidget}\PY{p}{>} \PY{n}{children}\PY{p}{;}
  \PY{k}{public} \PY{k+kt}{int} \PY{n}{Width} \PY{p}{\PYZob{}}
    \PY{k}{get} \PY{p}{\PYZob{}} \PY{k}{return} \PY{n}{children}\PY{p}{.}\PY{n}{Sum}\PY{p}{(}\PY{n}{x} \PY{p}{=}\PY{p}{>} \PY{n}{x}\PY{p}{.}\PY{n}{Width}\PY{p}{)}\PY{p}{;} \PY{p}{\PYZcb{}}
  \PY{p}{\PYZcb{}}
\PY{p}{\PYZcb{}}



\PY{k}{using} \PY{n+nn}{Patterns4Net.Attributes}\PY{p}{;}
\PY{n+na}{[Composite(typeof(IWidget))]}
\PY{k}{class} \PY{n+nc}{WidgetComposition} \PY{p}{:} \PY{n}{IWidget}\PY{p}{,} \PY{n}{ICloneable} \PY{p}{\PYZob{}}
  \PY{k}{private} \PY{n}{IList}\PY{p}{<}\PY{n}{IWidget}\PY{p}{>} \PY{n}{children}\PY{p}{;}
  \PY{k}{public} \PY{k+kt}{int} \PY{n}{Width} \PY{p}{\PYZob{}}
    \PY{k}{get} \PY{p}{\PYZob{}} \PY{k}{return} \PY{n}{children}\PY{p}{.}\PY{n}{Sum}\PY{p}{(}\PY{n}{x} \PY{p}{=}\PY{p}{>} \PY{n}{x}\PY{p}{.}\PY{n}{Width}\PY{p}{)}\PY{p}{;} \PY{p}{\PYZcb{}}
  \PY{p}{\PYZcb{}} \PY{c+c1}{// ...}
\PY{p}{\PYZcb{}}



\PY{k}{public} \PY{k}{class} \PY{n+nc}{Composite} \PY{p}{:} \PY{n}{IPattern} \PY{p}{\PYZob{}}
  \PY{k}{public} \PY{n}{TypeDefinition} \PY{n}{Composite} \PY{p}{\PYZob{}} \PY{k}{get}\PY{p}{;} \PY{k}{set}\PY{p}{;} \PY{p}{\PYZcb{}}
  \PY{k}{public} \PY{n}{TypeReference} \PY{n}{Component} \PY{p}{\PYZob{}} \PY{k}{get}\PY{p}{;} \PY{k}{set}\PY{p}{;} \PY{p}{\PYZcb{}}
  \PY{c+c1}{// The Name is required by IPatter interface}
  \PY{k}{public} \PY{k+kt}{string} \PY{n}{Name} \PY{p}{\PYZob{}} 
    \PY{k}{get} \PY{p}{\PYZob{}} \PY{k}{return} \PY{l+s}{"Composite"}\PY{p}{;} \PY{p}{\PYZcb{}} 
  \PY{p}{\PYZcb{}}
\PY{p}{\PYZcb{}}




\PY{n}{var} \PY{n}{mapper} \PY{p}{=} \PY{k}{new} \PY{n}{ModelMapper}\PY{p}{(}\PY{p}{)}\PY{p}{;}
\PY{n}{mapper}\PY{p}{.}\PY{n}{Class}\PY{p}{<}\PY{n}{RegisteredUser}\PY{p}{>}\PY{p}{(}\PY{n}{mapping} \PY{p}{=}\PY{p}{>}
\PY{p}{\PYZob{}}
  \PY{n}{mapping}\PY{p}{.}\PY{n}{Id}\PY{p}{(}\PY{n}{x} \PY{p}{=}\PY{p}{>} \PY{n}{x}\PY{p}{.}\PY{n}{Id}\PY{p}{,} 
    \PY{n}{map} \PY{p}{=}\PY{p}{>} \PY{n}{map}\PY{p}{.}\PY{n}{Column}\PY{p}{(}\PY{l+s}{"MyClassId"}\PY{p}{)}\PY{p}{)}\PY{p}{;}
  \PY{n}{mapping}\PY{p}{.}\PY{n}{Property}\PY{p}{(}\PY{n}{x} \PY{p}{=}\PY{p}{>} \PY{n}{x}\PY{p}{.}\PY{n}{Username}\PY{p}{,} 
    \PY{n}{map} \PY{p}{=}\PY{p}{>} \PY{n}{map}\PY{p}{.}\PY{n}{Length}\PY{p}{(}\PY{l+m}{1}\PY{l+m}{5}\PY{l+m}{0}\PY{p}{)}\PY{p}{)}\PY{p}{;}
\PY{p}{\PYZcb{}}\PY{p}{)}\PY{p}{;}





\PY{n}{mock}\PY{p}{.}\PY{n}{expects}\PY{p}{(}\PY{n}{once}\PY{p}{(}\PY{p}{)}\PY{p}{)}\PY{p}{.}\PY{n}{method}\PY{p}{(}\PY{l+s}{"m"}\PY{p}{)}
  \PY{p}{.}\PY{n}{with}\PY{p}{(}\PY{n}{stringContains}\PY{p}{(}\PY{l+s}{"hello"}\PY{p}{)}\PY{p}{)}\PY{p}{;}

  
  
  
\PY{c+c1}{// we want to work with the Composite role}
\PY{k}{this}\PY{p}{.}\PY{n}{Type}\PY{p}{(}\PY{n}{composite} \PY{p}{=}\PY{p}{>} \PY{n}{composite}\PY{p}{.}\PY{n}{Composite}\PY{p}{)}  
\PY{c+c1}{// we want to check all its non-private methods}
\PY{p}{.}\PY{n}{Methods}\PY{p}{(}\PY{n}{method} \PY{p}{=}\PY{p}{>} 
  \PY{n}{method}\PY{p}{.}\PY{n}{IsPublic} \PY{p}{|}\PY{p}{|} \PY{n}{method}\PY{p}{.}\PY{n}{IsProtected}\PY{p}{)} 
\PY{c+c1}{// on each of them perform the following check}
\PY{p}{.}\PY{n}{Check}\PY{p}{(}\PY{p}{(}\PY{n}{composite}\PY{p}{,} \PY{n}{method}\PY{p}{)} \PY{p}{=}\PY{p}{>} 
    \PY{n}{method}\PY{p}{.}\PY{n}{CallsAnyOf}\PY{p}{(}\PY{n}{pattern}\PY{p}{.}\PY{n}{Component}\PY{p}{)}\PY{p}{,}
  \PY{p}{(}\PY{n}{composite}\PY{p}{,} \PY{n}{method}\PY{p}{)} \PY{p}{=}\PY{p}{>} 
    \PY{l+s}{"error in "} \PY{p}{+} \PY{n}{method}\PY{p}{.}\PY{n}{Name}\PY{p}{)}\PY{p}{)}\PY{p}{;}

    
    
    
    
\PY{c+c1}{// check that declaring type is not sealed:}
\PY{k}{this}\PY{p}{.}\PY{n}{Type}\PY{p}{(}\PY{n}{pattern} \PY{p}{=}\PY{p}{>} 
  \PY{n}{pattern}\PY{p}{.}\PY{n}{TargetMethod}\PY{p}{.}\PY{n}{DeclaringType}\PY{p}{)}
  \PY{p}{.}\PY{n}{Check}\PY{p}{(}\PY{n}{type} \PY{p}{=}\PY{p}{>} \PY{n}{type}\PY{p}{.}\PY{n}{IsSealed} \PY{p}{=}\PY{p}{=} \PY{k}{false}\PY{p}{,}
         \PY{p}{(}\PY{n}{pattern}\PY{p}{,} \PY{n}{type}\PY{p}{)} \PY{p}{=}\PY{p}{>} \PY{l+s}{"...error message..."}\PY{p}{)}\PY{p}{;}

\PY{c+c1}{// check that template method invokes at least }
\PY{c+c1}{// one virtual method or another template method:}
\PY{k}{this}\PY{p}{.}\PY{n}{If}\PY{p}{(}\PY{n}{pattern} \PY{p}{=}\PY{p}{>} 
  \PY{p}{!}\PY{n}{pattern}\PY{p}{.}\PY{n}{TargetMethod}\PY{p}{.}\PY{n}{DeclaringType}\PY{p}{.}\PY{n}{IsSealed}\PY{p}{)}
  \PY{p}{.}\PY{n}{Method}\PY{p}{(}\PY{n}{pattern} \PY{p}{=}\PY{p}{>} \PY{n}{pattern}\PY{p}{.}\PY{n}{TargetMethod}\PY{p}{)}
  \PY{p}{.}\PY{n}{Check}\PY{p}{(}
    \PY{n}{method} \PY{p}{=}\PY{p}{>} 
      \PY{n}{method}\PY{p}{.}\PY{n}{GetMethodCalls}\PY{p}{(}\PY{p}{)}\PY{p}{.}\PY{n}{Any}\PY{p}{(}
        \PY{n}{call} \PY{p}{=}\PY{p}{>}
          \PY{n}{call}\PY{p}{.}\PY{n}{TargetObject} \PY{p}{!}\PY{p}{=} \PY{k}{null} \PY{p}{&}\PY{p}{&}
          \PY{n}{call}\PY{p}{.}\PY{n}{TargetObject}\PY{p}{.}\PY{n}{IsThisParameter} \PY{p}{&}\PY{p}{&}
          \PY{p}{(}\PY{n}{IsTemplateMethod}\PY{p}{(}\PY{n}{call}\PY{p}{.}\PY{n}{Method}\PY{p}{)} \PY{p}{|}\PY{p}{|}
           \PY{n}{call}\PY{p}{.}\PY{n}{Method}\PY{p}{.}\PY{n}{IsOverrideable}\PY{p}{(}\PY{p}{)}\PY{p}{)}\PY{p}{)}\PY{p}{,}
    \PY{p}{(}\PY{n}{pattern}\PY{p}{,} \PY{n}{method}\PY{p}{)} \PY{p}{=}\PY{p}{>} \PY{l+s}{"...error message..."}\PY{p}{)}\PY{p}{;}

  
  
  
  
  
\PY{k}{using} \PY{n+nn}{Patterns4Net.Attributes}\PY{p}{;}
\PY{n+na}{[Composite(typeof(IWidget))]}
\PY{k}{class} \PY{n+nc}{WidgetComposition} \PY{p}{:} \PY{n}{IWidget}\PY{p}{,} \PY{n}{ICloneable} \PY{p}{\PYZob{}}
  \PY{c+c1}{// ...}





\PY{k}{public} \PY{k}{abstract} \PY{k}{class} \PY{n+nc}{InstructionMatcher} \PY{p}{\PYZob{}}
  \PY{k}{public} \PY{k}{virtual} \PY{k+kt}{bool} \PY{n}{Found} \PY{p}{\PYZob{}} \PY{k}{get}\PY{p}{;} \PY{k}{set}\PY{p}{;} \PY{p}{\PYZcb{}}
  \PY{k}{public} \PY{k}{abstract} \PY{k+kt}{bool} \PY{n+nf}{Matches}\PY{p}{(}
    \PY{n}{Instruction} \PY{n}{instruction}\PY{p}{,} 
    \PY{k}{out} \PY{n}{Instruction} \PY{n}{next}\PY{p}{)}\PY{p}{;}
  \PY{k}{public} \PY{k}{virtual} \PY{k}{void} \PY{n+nf}{Reset}\PY{p}{(}\PY{p}{)} \PY{p}{\PYZob{}} \PY{p}{.}\PY{p}{.}\PY{p}{.} \PY{p}{\PYZcb{}}
\PY{p}{\PYZcb{}}
\end{Verbatim}

\end{document}
